import { Graphical } from '../Utils.Graphical/UtilsGraphical';
import UtilsSprite from '../Utils.Sprite/UtilsSprite';
import UtilsDetection from '../Utils.Detection/UtilsDetection';

const _overlapPlayer = Symbol('_overlapPlayer');
const _Workflow = Symbol('_Workflow');
const _error = Symbol('_error');
export default class UtilsItemDrops {
    constructor(_options, _that) {
        if (!_options) return this[_error]('no options?');
        this.options = _options;
        this[_Workflow](_that);
    }

    /**
     * @summary 装备掉落工作流
     */
    [_Workflow](_that) {
        /**
         * @summary 目标坐标
         */
        let tarXY = {
            x: _that[this.options.tarKey].x,
            y: _that[this.options.tarKey].y
        }
        _that[this.options.key].x = tarXY.x;
        _that[this.options.key].y = tarXY.y - this.options.off.y;

        /**
         * @summary 绘画底座
         */
        _that.add.existing(new Graphical({
            path: false,
            style: {
                col: 0xff0000,
                alp: .8
            },
            rec: {
                x: tarXY.x, 
                y: tarXY.y,
                w: 100,
                h: 10
            },
            gen: {
                genKey: this.options.grap.genKey,
                w: 100,
                h: 10
            },
            vis: true,
            des: true
        }, _that));

        /**
         * @summary 绘画底座生成为精灵
         */
        new UtilsSprite([{
            key: this.options.grap.genSpiritKey,
            spriteKey: this.options.grap.genKey,
            sprXY: {
                x: tarXY.x, 
                y: tarXY.y
            }
        }], 'static', _that);

        /**
         * @summary 监听掉落与底座
         */
        new UtilsDetection({
            type: '_collider',
            key: this.options.grap.genSpiritKey,
            targetKey: this.options.key
        }, (e) => {
            // console.log(e);
        }, _that);

        /**
         * @summary 物理重力
         */
        _that[this.options.key].setVisible(true);
        _that[this.options.key].body.setAllowGravity(true);
        _that[this.options.key].setBounce(this.options.boun);

        this.options.open.overlap ? this[_overlapPlayer](_that) : null;
    }

    [_overlapPlayer](_that) {
        /**
         * @summary 监听掉落与player
         */
         new UtilsDetection({
            type: '_overlap',
            key: this.options.open.overTarKey,
            targetKey: this.options.key
        }, (e) => {
            // console.log(e);
            _that[this.options.key].destroy(true, true);
            _that[this.options.grap.genSpiritKey].destroy(true, true);
        }, _that);
    }

    [_error](err) {
        throw err;
    }
}